package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apiario;
import com.model.Apicultor;
import com.model.Exame;

import com.model.RegEnxames;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table REG_ENXAMES.
*/
public class RegEnxamesDao extends AbstractDao<RegEnxames, Long> {

    public static final String TABLENAME = "REG_ENXAMES";

    /**
     * Properties of entity RegEnxames.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idRegEnxames");
        public final static Property Ano = new Property(1, String.class, "Ano", false, "ANO");
        public final static Property DataEnxemeacao = new Property(2, java.util.Date.class, "dataEnxemeacao", false, "DATA_ENXEMEACAO");
        public final static Property Discricao = new Property(3, String.class, "Discricao", false, "DISCRICAO");
        public final static Property ColmOriginada = new Property(4, String.class, "ColmOriginada", false, "COLM_ORIGINADA");
        public final static Property Tipoenxame = new Property(5, Integer.class, "tipoenxame", false, "TIPOENXAME");
        public final static Property Tipodesdobra = new Property(6, Integer.class, "tipodesdobra", false, "TIPODESDOBRA");
        public final static Property Sucesso = new Property(7, Integer.class, "sucesso", false, "SUCESSO");
        public final static Property ColmOrigemId = new Property(8, long.class, "colmOrigemId", false, "COLM_ORIGEM_ID");
        public final static Property ApicultorId = new Property(9, long.class, "apicultorId", false, "APICULTOR_ID");
        public final static Property ApiarioId = new Property(10, long.class, "apiarioId", false, "APIARIO_ID");
        public final static Property Date = new Property(11, java.util.Date.class, "date", false, "DATE");
    };

    private DaoSession daoSession;

    private Query<RegEnxames> apiario_ApiarioRegExamesQuery;
    private Query<RegEnxames> apicultor_ApicultorRegExamesQuery;
    private Query<RegEnxames> apicultor_ExameOrigemRegExamesQuery;

    public RegEnxamesDao(DaoConfig config) {
        super(config);
    }
    
    public RegEnxamesDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'REG_ENXAMES' (" + //
                "'_idRegEnxames' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'ANO' TEXT," + // 1: Ano
                "'DATA_ENXEMEACAO' INTEGER," + // 2: dataEnxemeacao
                "'DISCRICAO' TEXT," + // 3: Discricao
                "'COLM_ORIGINADA' TEXT," + // 4: ColmOriginada
                "'TIPOENXAME' INTEGER," + // 5: tipoenxame
                "'TIPODESDOBRA' INTEGER," + // 6: tipodesdobra
                "'SUCESSO' INTEGER," + // 7: sucesso
                "'COLM_ORIGEM_ID' INTEGER NOT NULL ," + // 8: colmOrigemId
                "'APICULTOR_ID' INTEGER NOT NULL ," + // 9: apicultorId
                "'APIARIO_ID' INTEGER NOT NULL ," + // 10: apiarioId
                "'DATE' INTEGER);"); // 11: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'REG_ENXAMES'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, RegEnxames entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        String Ano = entity.getAno();
        if (Ano != null) {
            stmt.bindString(2, Ano);
        }
 
        java.util.Date dataEnxemeacao = entity.getDataEnxemeacao();
        if (dataEnxemeacao != null) {
            stmt.bindLong(3, dataEnxemeacao.getTime());
        }
 
        String Discricao = entity.getDiscricao();
        if (Discricao != null) {
            stmt.bindString(4, Discricao);
        }
 
        String ColmOriginada = entity.getColmOriginada();
        if (ColmOriginada != null) {
            stmt.bindString(5, ColmOriginada);
        }
 
        Integer tipoenxame = entity.getTipoenxame();
        if (tipoenxame != null) {
            stmt.bindLong(6, tipoenxame);
        }
 
        Integer tipodesdobra = entity.getTipodesdobra();
        if (tipodesdobra != null) {
            stmt.bindLong(7, tipodesdobra);
        }
 
        Integer sucesso = entity.getSucesso();
        if (sucesso != null) {
            stmt.bindLong(8, sucesso);
        }
        stmt.bindLong(9, entity.getColmOrigemId());
        stmt.bindLong(10, entity.getApicultorId());
        stmt.bindLong(11, entity.getApiarioId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(12, date.getTime());
        }
    }

    @Override
    protected void attachEntity(RegEnxames entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public RegEnxames readEntity(Cursor cursor, int offset) {
        RegEnxames entity = new RegEnxames( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // Ano
            cursor.isNull(offset + 2) ? null : new java.util.Date(cursor.getLong(offset + 2)), // dataEnxemeacao
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // Discricao
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // ColmOriginada
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // tipoenxame
            cursor.isNull(offset + 6) ? null : cursor.getInt(offset + 6), // tipodesdobra
            cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7), // sucesso
            cursor.getLong(offset + 8), // colmOrigemId
            cursor.getLong(offset + 9), // apicultorId
            cursor.getLong(offset + 10), // apiarioId
            cursor.isNull(offset + 11) ? null : new java.util.Date(cursor.getLong(offset + 11)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, RegEnxames entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setAno(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setDataEnxemeacao(cursor.isNull(offset + 2) ? null : new java.util.Date(cursor.getLong(offset + 2)));
        entity.setDiscricao(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setColmOriginada(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setTipoenxame(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setTipodesdobra(cursor.isNull(offset + 6) ? null : cursor.getInt(offset + 6));
        entity.setSucesso(cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7));
        entity.setColmOrigemId(cursor.getLong(offset + 8));
        entity.setApicultorId(cursor.getLong(offset + 9));
        entity.setApiarioId(cursor.getLong(offset + 10));
        entity.setDate(cursor.isNull(offset + 11) ? null : new java.util.Date(cursor.getLong(offset + 11)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(RegEnxames entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(RegEnxames entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "apiarioRegExames" to-many relationship of Apiario. */
    public List<RegEnxames> _queryApiario_ApiarioRegExames(long apiarioId) {
        synchronized (this) {
            if (apiario_ApiarioRegExamesQuery == null) {
                QueryBuilder<RegEnxames> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApiarioId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apiario_ApiarioRegExamesQuery = queryBuilder.build();
            }
        }
        Query<RegEnxames> query = apiario_ApiarioRegExamesQuery.forCurrentThread();
        query.setParameter(0, apiarioId);
        return query.list();
    }

    /** Internal query to resolve the "apicultorRegExames" to-many relationship of Apicultor. */
    public List<RegEnxames> _queryApicultor_ApicultorRegExames(long apicultorId) {
        synchronized (this) {
            if (apicultor_ApicultorRegExamesQuery == null) {
                QueryBuilder<RegEnxames> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ApicultorId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_ApicultorRegExamesQuery = queryBuilder.build();
            }
        }
        Query<RegEnxames> query = apicultor_ApicultorRegExamesQuery.forCurrentThread();
        query.setParameter(0, apicultorId);
        return query.list();
    }

    /** Internal query to resolve the "exameOrigemRegExames" to-many relationship of Apicultor. */
    public List<RegEnxames> _queryApicultor_ExameOrigemRegExames(long colmOrigemId) {
        synchronized (this) {
            if (apicultor_ExameOrigemRegExamesQuery == null) {
                QueryBuilder<RegEnxames> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.ColmOrigemId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_ExameOrigemRegExamesQuery = queryBuilder.build();
            }
        }
        Query<RegEnxames> query = apicultor_ExameOrigemRegExamesQuery.forCurrentThread();
        query.setParameter(0, colmOrigemId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApiarioDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getApicultorDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getExameDao().getAllColumns());
            builder.append(" FROM REG_ENXAMES T");
            builder.append(" LEFT JOIN APIARIO T0 ON T.'APIARIO_ID'=T0.'_idApiario'");
            builder.append(" LEFT JOIN APICULTOR T1 ON T.'APICULTOR_ID'=T1.'_idApicultor'");
            builder.append(" LEFT JOIN EXAME T2 ON T.'COLM_ORIGEM_ID'=T2.'_idExame'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected RegEnxames loadCurrentDeep(Cursor cursor, boolean lock) {
        RegEnxames entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apiario apiario = loadCurrentOther(daoSession.getApiarioDao(), cursor, offset);
         if(apiario != null) {
            entity.setApiario(apiario);
        }
        offset += daoSession.getApiarioDao().getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }
        offset += daoSession.getApicultorDao().getAllColumns().length;

        Exame exame = loadCurrentOther(daoSession.getExameDao(), cursor, offset);
         if(exame != null) {
            entity.setExame(exame);
        }

        return entity;    
    }

    public RegEnxames loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<RegEnxames> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<RegEnxames> list = new ArrayList<RegEnxames>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<RegEnxames> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<RegEnxames> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
